package org.example.JUC.stop;

public class myStopThread extends Thread{

    private boolean stop=false;

    public myStopThread(String threadName){
        super(threadName);
    }


    public void run(){

        for(int j=0;j<100;j++){
            if(stop){
                System.out.println(Thread.currentThread().getName()+":"+j+"is stopped");
                break;
            }
            try {
                doSomeThing(j);
            }catch (InterruptedException e){
                break;
            }
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
                break;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("Exception,继续");
            }
        }
    }

    private void doSomeThing(int j) throws InterruptedException {
        if(Thread.interrupted()){
            throw new InterruptedException();
        }
        System.out.println(Thread.currentThread().getName()+":"+j);
        return;
    }

    public void setStop(){
        this.stop=true;
    }


}
